package parasoft.centrap.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;

public class FileFinder {
	private static String name = "";
	private static String path = "";
	private static String packagep = "";
	private static final FileFinder INSTANCE = new FileFinder();
	private static boolean founded = false;
	private static String ext = "";
	private static Vector files = new Vector();

	private FileFinder(){
	}

	public static FileFinder getI(){
		return INSTANCE;
	}

	public static boolean find(String plik){
		return find(plik,"/usr/local/CentralReports/webapps/CentralReports/WEB-INF/classes/");
	}
	public static boolean find(String plik, String sciezka){
		return find(plik,sciezka,ext);
	}

	public static boolean find(String plik, String sciezka, String eext){
		name = plik;
		if(eext!=null){
			ext = eext;
		}
		path = sciezka;

		Process p = null;
		try {
			p = Runtime.getRuntime().exec("locate "+plik);
		} catch (IOException e1) {
			standardSearching(sciezka, plik);
		}
		try {
		    BufferedReader bisr = new BufferedReader(new InputStreamReader(p.getInputStream()));
		    String line = "";
		    while((line = bisr.readLine())!=null){
		    	if(line.trim().length()>0 && (line.indexOf(path)!=-1)&& (line.indexOf(plik)!=-1) && line.endsWith(ext)){
		    		files.add(line.trim());
		    	}
		    }
		    if(files.size()>0){
		    	founded = true;
		    }
		    bisr.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return founded;
	}

	public String getName() {
		return name;
	}


	public String getFileName() {
		String fn = new String(name);
		if(!fn.toLowerCase().endsWith(ext)){
			fn += ext;
		}
		return fn;
	}

	public String getPath() {
		return path;
	}

	public String getClassPath() {
		return packagep.replaceAll("/", ".");
	}

	public String getPackage() {
		int indo = packagep.indexOf(name);
		if(packagep.trim().length()>0 && indo >-1){
			return packagep.replaceAll("/", ".").substring(0,packagep.indexOf(name)-1);
		}else{
			return packagep.replaceAll("/", ".");
		}
	}

	public Vector getFiles(){
		return files;
	}

	public void printFiles(){
		for (int i = 0; i < files.size(); i++) {
			System.out.println(files.get(i));
		}
	}

	public static void standardSearching(String sciezka, String plik){
		founded = false;
		try{
			if(plik!=null && plik.indexOf(".")!=-1){
				name = plik.substring(0, plik.indexOf("."));
			}else{
				name = plik;
			}
			final File start =  new File(sciezka);
			if(start.exists()){
				searchDir(1, new File(sciezka));
			}
		}catch(IOException ioe){
			ioe.printStackTrace();
		}
	}

	public static void searchDir(int indent, File file) throws IOException {
		  String p = file.getAbsolutePath();
		  path = p;
			  if (!file.isDirectory()) {
				  if(file.getName().equalsIgnoreCase(name+ext)){
					  founded = true;
				  }
				  if(p.indexOf("classes")!=-1){
					  p = p.substring(p.indexOf("classes")+8);
				  }
				  p = p.replaceAll(ext, "");
				  if(p!=null && p.trim().length()>0){
					  packagep = p;
				  }
			  }
		    if (file.isDirectory()) {
				File[] files = file.listFiles();
				if(files!=null){
					for (int i = 0; i < files.length && !(founded); i++)
				    	searchDir(indent + 4, files[i]);
				}
		    }
		    if(founded){
		    	return;
		    }
	  }

	public String toString() {
		String out = "name:"+getName()+"\n"+
		"path:"+getPath()+"\n"+
		"class:"+getClassPath()+"\n"+
		"package:"+getPackage()+"\n"+
		   founded+"\n"+
		   ext+"";
		for (int i = 0; i < files.size(); i++) {
			out += files.get(i)+"\n";
		}
		return out;
	}
}
